package com.nd.mobile.study.view;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.mobclick.android.MobclickAgent;
import com.nd.mobile.study.R;
import com.nd.mobile.study.common.constants.SharedPreferenceConstants;
import com.nd.mobile.study.common.util.NetworkUtil;
import com.nd.mobile.study.common.util.SharedPreferencesUtil;
import com.nd.mobile.study.view.helper.AnimHelper;

public class TabMainActivity extends TabActivity {

    private boolean isFullScreen = false;

    SharedPreferencesUtil sp;

    Intent mIntent = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferencesUtil sp = SharedPreferencesUtil.getSyscfgSp(this);
        isFullScreen = sp.getBoolean(SharedPreferenceConstants.SYSCFG_FULLLSCREEN);
        if (sp != null) {
            Log.d("MainActivity onCreate", sp.toString());
        }
        if (isFullScreen) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        }

        // 创建TabHost
        setContentView(R.layout.tab);
        TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();

        // 首页
        TabSpec ts_practise = tabHost.newTabSpec("home");
        ts_practise.setIndicator(createIndicator(R.drawable.indicator_home, "首页"));
        ts_practise.setContent(new Intent(this, HomeActivity.class));
        tabHost.addTab(ts_practise);
        
        // 资讯
        TabSpec ts_storage = tabHost.newTabSpec("items");
        ts_storage.setIndicator(createIndicator(R.drawable.indicator_info, "资讯"));
        ts_storage.setContent(new Intent(this, NewsListActivity.class));
        tabHost.addTab(ts_storage);

        // 应用
        TabSpec ts_down = tabHost.newTabSpec("marks");
        ts_down.setIndicator(createIndicator(R.drawable.indicator_app, "应用"));
        Intent downIntent = new Intent(this, AppActivity.class);
        ts_down.setContent(downIntent);
        tabHost.addTab(ts_down);

        // 更多
        TabSpec ts_more = tabHost.newTabSpec("more");
        ts_more.setIndicator(createIndicator(R.drawable.indicator_more, "更多"));
        ts_more.setContent(new Intent(this, MoreActivity.class));
        tabHost.addTab(ts_more);

        // 跳出选中当前进去的tab
        mIntent = getIntent();
        int index = mIntent.getIntExtra("index", -1);
        if (index != -1) {
            tabHost.setCurrentTab(index);
        }
    }

    /**
     * 创建标签页
     * 
     * @param image
     *            背景图片
     * @param text
     *            显示文字
     * @return 标签叶的View
     */
    private View createIndicator(int image, String text) {
        // 获取整个标签页的view
        View view = getLayoutInflater().inflate(R.layout.tab_item, null);
        // 设置背景色
        RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.relativeLayout);
        relativeLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_selector));
        // 设置图片
        ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
        imageView.setBackgroundDrawable(getResources().getDrawable(image));
        // 设置文字
        TextView textView = (TextView) view.findViewById(R.id.textView);
        textView.setText(text);

        AnimHelper.setViewTouchAnim(view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

}
